﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using EXCEL = Microsoft.Office.Interop.Excel;
using Common = PCS.Common;


namespace PCS
{
    public partial class PCS
    {
        //change log:
        //Revision 0.1 new.
        //Revision 0.2 1/14/2013 distribute work effort to all resources the task assigned to.


        EXCEL.Application app;

        Util.ExcelUtil excelUtil;

        private void PCS_Load(object sender, RibbonUIEventArgs e)
        {
            app = Globals.ThisAddIn.Application;
        }

        private void button1_Click(object sender, RibbonControlEventArgs e)
        {

            string original_name;
            string resource_name;
            System.DateTime start_date;
            System.DateTime finish_date;
            Dictionary<System.DateTime, System.DateTime> dict;

            int i = Common.Const.ROW_START;

            //string[] Day = new string[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
            string[] Mon = new string[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
            string month_name;

            //Revision 2: distribute work effort to all resources the task assigned to.
            decimal avg_effort;
            decimal last_effort;
            decimal total_effort;

            int pos;
            int total_workdays;

            EXCEL.Worksheet ws = (EXCEL.Worksheet)app.ActiveSheet;
            excelUtil = new Util.ExcelUtil();
            excelUtil.app = app;

            //
            while (ws.Cells[i, Common.Const.TASK_NAME_COLUMN].value != null)
            {
                if (ws.Cells[i, Common.Const.RESOURCE_NAME_COLUMN].value == null)
                {
                    i++;
                    continue;
                }

                start_date = System.DateTime.Parse(ws.Cells[i, Common.Const.START_DATE_COLUMN].value.ToString());
                finish_date = System.DateTime.Parse(ws.Cells[i, Common.Const.FINISH_DATE_COLUMN].value.ToString());

                //divide date region by monthly.
                if (Util.DateUtil.DateDiff(start_date, finish_date) > 31)
                {
                    dict = Util.DateUtil.DivideDateRegions(start_date, finish_date);
                }
                else
                {
                    dict = new Dictionary<DateTime, DateTime>();
                    dict.Add(start_date, finish_date);
                }

                //Revision 2:
                total_workdays = Util.DateUtil.TotalWorkday(start_date, finish_date);

                original_name = ws.Cells[i, Common.Const.TASK_NAME_COLUMN].value;

                resource_name = ws.Cells[i, Common.Const.RESOURCE_NAME_COLUMN].value.ToString();
                //string [] resources = resource_name.Split(new Char [] {'，'});

                string[] resources = resource_name.Split(new String[] { Common.Const.SPLITTER_COMMAS_CN, Common.Const.SPLITTER_RETRUN, Common.Const.SPLITTER_SEMICOLON_EN, Common.Const.SPLITTER_SEMICOLON_CN }, StringSplitOptions.RemoveEmptyEntries);

                //remove blank.
                for (int idx = 0; idx < resources.Count(); idx++)
                {
                    resources[idx] = resources[idx].Trim();
                }


                //only one resource and only one date region (duration no less than one month.)
                if (resources.Count() == 1 && dict.Count == 1)
                {
                    i++;
                    continue;
                }


                //added 1/8/2013
                //clear resource name info
                ws.Cells[i, Common.Const.RESOURCE_NAME_COLUMN].value = "";

                //Revision 2 start: distribute work effort to all resources the task assigned to.
                if (ws.Cells[i, Common.Const.WORK_COLUMN].text != "")
                    total_effort = decimal.Parse(ws.Cells[i, Common.Const.WORK_COLUMN].text);
                else
                    total_effort = 0;

                ws.Cells[i, Common.Const.WORK_COLUMN].value = "";

                if (resources.Count() > 1 && total_effort.CompareTo(0) != 0)
                {
                    avg_effort = Math.Floor((decimal)total_effort / resources.Count());
                    last_effort = total_effort - avg_effort * (resources.Count() - 1);
                }
                else
                {
                    avg_effort = total_effort;
                    last_effort = total_effort;
                }
                //Revision 2 finish
                pos = 1;
                foreach (string s in resources)
                {

                    //
                    decimal temp_effort;
                    if (pos != resources.Count())
                    {
                        temp_effort = avg_effort;
                    }
                    else
                    {
                        temp_effort = last_effort;
                    }

                    int date_pos = 1;
                    decimal new_effort = 0;
                    decimal total_assigned_effort = 0;
                    foreach (System.DateTime d in dict.Keys)
                    {
                        int total_workday_new;
                        bool flgOneResource;
                        
                        month_name = Mon[Convert.ToInt16(d.Month - 1)];
                        System.DateTime start_new = Util.DateUtil.ChangeNonHoliday(d, Common.DATETYPE.START);
                        System.DateTime finish_new = Util.DateUtil.ChangeNonHoliday(dict[d], Common.DATETYPE.FINISH);
                        if (dict.Count == 1)
                        {
                            month_name = "";
                        }

                        string res;
                        res = s;
                        if (resources.Count() == 1)
                        {
                            flgOneResource = true;
                        }
                        else
                        {
                            flgOneResource = false;
                        }

                        total_workday_new = Util.DateUtil.TotalWorkday(start_new, finish_new);

                        if (date_pos != dict.Count)
                        {
                            new_effort = Math.Floor((decimal)total_workday_new / total_workdays * temp_effort);
                            total_assigned_effort = total_assigned_effort + new_effort;
                        }
                        else
                        {
                            new_effort = temp_effort - total_assigned_effort;

                        }

                        excelUtil.Insert(i, original_name, month_name, flgOneResource, res, start_new, finish_new, new_effort);
                        
                        i++;
                        date_pos++;
                    }

                    pos++;
                }
                i++;
            }
            System.Windows.Forms.MessageBox.Show("Completed.");

        }

        private void button2_Click(object sender, RibbonControlEventArgs e)
        {

                EXCEL.Worksheet ws = (EXCEL.Worksheet)app.ActiveSheet;

                excelUtil = new Util.ExcelUtil();
                excelUtil.app = app;

                if (ws.Name != "Project Info - by PM")
	            {
                    System.Windows.Forms.MessageBox.Show("Please select Project Info - by PM sheet firstly.");
		            return;
	            }

                for (int i = app.Selection.Row; i < app.Selection.Rows.Count + app.Selection.Row; i++)
			    {

                    ws.Cells[i,10].value = Util.DateUtil.TotalWorkday(ws.Cells[i, Common.Const.ROLL_ON_DATE_COLUMN].value, ws.Cells[i, Common.Const.ROLL_OFF_DATE_COLUMN].value);
			 
			    }

        }
    }
}
